package net.chathu.airgw.model.handler 
{
	import net.chathu.airgw.Debug;
	import net.chathu.airgw.model.SessionData;
	import net.chathu.airgw.packets.chat.*;
	
	/**
	 * ...
	 * @author ze
	 */
	public class ChatPacketProcessor 
	{
		
		public static function handlePacket(packetdata:Array, session:SessionData):void
		{
			var secondary:String;
			if (packetdata.length >= 2)
			{
				secondary = packetdata[1];
			}
			var packet:ChatPacket;
			switch(packetdata[0])
			{
				case "auth_ok":
					packet = new AuthOk();
				break;
				case "auth_fail":
					packet = new AuthFail();
				break;
				case "iadd":
					if ( secondary == "users") {
						packet = new IAddUsers();
					} else if ( secondary.indexOf("rooms")==0 ) {
						packet = new IAddRooms();
					} else if ( secondary =="confrooms" ) {
						packet = new IAddConfRooms();
					} else if ( secondary == "private") {
						packet = new IAddPrivate();
					}
				break;
				case "iadds":
					if( secondary == "users" ) {
						packet = new IAddsUsers();
					}
				break;
				case "ichg":
					if ( secondary == "rooms" ) {
						packet = new IChgRooms();
					} else if (secondary == "users") {
						packet = new IChgUsers();
					} else if (secondary == "confrooms") {
						packet = new IChgConfRooms();
					}
				break;
				case "irem":
					if ( secondary == "users") {
						packet = new IRemUsers();
					} else if ( secondary.indexOf("rooms")==0 ) {
						packet = new IRemRooms();
					} else if ( secondary == "private" ) {
						packet = new IRemPrivate();
					} else if ( secondary =="confrooms" ) {
						packet = new IRemConfRooms();
					} 
				break;
				case "items":
					if ( secondary.indexOf("rooms")==0 ) {
						//if ((words[1]).equals("rooms"))
						//{...
						packet = new ItemsRooms();
					} else if( secondary == "confrooms" ) {
						packet = new ItemsConfRooms();
					} else if( secondary == "users" ) {
						packet = new ItemsUsers();
					} 
				break;
				case "m":
					if ( secondary.indexOf("rooms")==0 ) {
						packet = new MRooms();
					} else if (secondary.indexOf("private")==0) {
						packet = new MPrivate();
					} else if (secondary =="control") {
						packet = new MControl();
					} else if (secondary =="userinfo") {
						packet = new MUserinfo();
					} else if (secondary =="messages") {
						packet = new MMessages();
					} 
				break;
				case "roomtreestate":
					packet = new Roomtreestate();
				break;
				case "roomrights":
					packet = new Roomrights();
				break;
				case "sub":
					//if ( secondary.indexOf("rooms/")==0 && words[1].endsWith("/chat") ) {
					if ( secondary.indexOf("rooms/")==0 && secondary.substr(secondary.length-5,5)=="/chat") {
						packet = new SubRooms();
					} else if (secondary.indexOf("rooms/") == 0 && secondary.substr(secondary.length - 6, 6) == "/users") {
						//NOOP
						packet = new DummyPacket();
					} else if ( secondary.indexOf("private/")==0 ) {
						packet = new SubPrivate();
					} else if ( secondary =="userinfo" ) {
						//NOOP
						packet = new DummyPacket();
					} else if ( secondary =="confrooms" ) {
						//NOOP
						packet = new DummyPacket();
					} else if ( secondary =="rooms" ) {
						//NOOP
						packet = new DummyPacket();
					} else if ( secondary =="users" ) {
						//NOOP
						packet = new DummyPacket();
					} else if ( secondary =="private" ) {
						//NOOP
						packet = new DummyPacket();
					} else if ( secondary =="messages" ) {
						//NOOP
						packet = new DummyPacket();
					}
				break;
				case "sdeny":
					packet = new SDeny();
				break;
				case "time":
					packet = new Time();
				break;
				case "unsub":
					if ( secondary.indexOf("rooms/")==0 && secondary.substr(secondary.length-5,5)=="/chat")
					{
						packet = new UnsubRooms();
					} else if ( secondary.indexOf("private/")==0 ) {
						packet = new UnsubPrivate();
					} else {
						packet = new DummyPacket();
					}
				break;
			}
			if (packet)
			{
				packet.sessionData = session;
				packet.data = packetdata;
				packet.execute();
			} else {
				Debug.echo("UNHANDLED PACKET: " + packetdata.join(" "));
			}
			
		}
		
	}

}